import sys, os
from cmd import Cmd
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))

from bitbrook.peer_facade import PeerFacade

class CmdInterpreter(Cmd):
    def __init__(self):
        Cmd.__init__(self)
        
        self.peer = PeerFacade(sys.argv[1], sys.argv[2], sys.argv[3])
        self.peer.start()
        
    def do_exit(self, args):
        print "Shutting down client...",
        self.peer.shutdown()
        self.peer.join()
        sys.exit(0)

cmdint = CmdInterpreter()
try:
    cmdint.cmdloop("Welcome to BBClient.")
except KeyboardInterrupt:
    print "Keyboard interrupt caught. Shutting down client...",
    cmdint.peer.shutdown()
    cmdint.peer.join()
    sys.exit(0)